/**
 * Created by shidaiapp on 2017/9/29.
 */
import Vue from "vue";
import {formatDate, formatLocaleDate} from "../../../js/dateUtils";
import {isEmpty} from "../../../js/commonUtils";

export const init = (tiyData) => {
  tiyData.tData.userDailyList.splice(0, tiyData.tData.userDailyList.length);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 60;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.searchParams.date = "";
  tiyData.tParams.searchParams.isGetAll = false;
  tiyData.tParams.searchParams.orderKey = "";
  tiyData.tParams.searchParams.orderType = "";
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const searchService = (self, tiyData) => {
  self.$Spin.show();
  let date = isEmpty(tiyData.tParams.searchParams.date)?"":formatLocaleDate(tiyData.tParams.searchParams.date, "yyyy-MM-dd");
  self.$apiAnalysis.userDailyList({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "date" : date,
    "isGetAll" : tiyData.tParams.searchParams.isGetAll,
    "orderKey" : tiyData.tParams.searchParams.orderKey,
    "orderType" : tiyData.tParams.searchParams.orderType
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let userPage = response.result;
      tiyData.tPage.total = userPage.total;
      tiyData.tData.userDailyList.splice(0, tiyData.tData.userDailyList.length);
      userPage.list.forEach(
        function (item) {
          tiyData.tData.userDailyList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};
